package com.ego.service.impl;

import com.ego.dubbo.service.ContentCategoryDubboService;
import com.ego.exception.DaoException;
import com.ego.pojo.ContentCategory;
import com.ego.pojo.EasyUITree;
import com.ego.pojo.EgoResult;
import com.ego.service.ContentCategoryService;
import com.ego.utils.IDUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @DubboReference
    private ContentCategoryDubboService contentCategoryDubboService;

    @Override
    public List<EasyUITree> selectContentCategoryByParenId(Long id) {
        List<EasyUITree> easyUITrees = new ArrayList<>();
        List<ContentCategory> contentCategories = contentCategoryDubboService.selectContentCategoryByParentId(id);
        for (ContentCategory contentCategory : contentCategories) {
            EasyUITree easyUITree = new EasyUITree();
            easyUITree.setId(contentCategory.getId());
            easyUITree.setText(contentCategory.getName());
            easyUITree.setState(contentCategory.getIsParent() ? "closed" : "open");
            easyUITrees.add(easyUITree);
        }
        return easyUITrees;
    }

    @Override
    public EgoResult saveContentCategory(Long parentId, String name) {
        //判断新建分类的name在该父内容中是否已存在
        List<ContentCategory> contentCategories = contentCategoryDubboService.selectContentCategoryByParentId(parentId);
        for (ContentCategory contentCategory : contentCategories) {
            if (contentCategory.getName().equals(name)) {
                return EgoResult.error("新增节点名已存在");
            }
        }

        Date now = new Date();
        ContentCategory son = new ContentCategory();
        son.setId(IDUtils.genItemId());
        son.setParentId(parentId);
        son.setIsParent(false);
        son.setName(name);
        son.setCreated(now);
        son.setUpdated(now);
        son.setSortOrder(1);

        try {
            int i = contentCategoryDubboService.saveContentCategory(son);
            if (i == 1) {
                return EgoResult.ok(son);
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("新建内容分类失败");
    }

    @Override
    public EgoResult updateContentCategory(Long id, String name) {
        //1.验证该名称是否和当前父节点的其他子节点有同名
        //获取当前节点对象
        ContentCategory currentContentCategory = contentCategoryDubboService.selectContentCategoryById(id);
        //获取前父节点的其他子节点的集合
        List<ContentCategory> contentCategoryList = contentCategoryDubboService.selectContentCategoryByParentId(currentContentCategory.getParentId());
        for (ContentCategory contentCategory : contentCategoryList) {
            if (!contentCategory.getId().equals(currentContentCategory.getId())){
                if (contentCategory.getName().equals(name)){
                    //说明名称是存在
                    return EgoResult.error("名称已经存在");
                }
            }
        }
        //2.重命名
        currentContentCategory.setId(id);
        currentContentCategory.setName(name);
        currentContentCategory.setUpdated(new Date());
        int result = contentCategoryDubboService.updateContentCategoryById(currentContentCategory);
        if (result == 1) {
            return EgoResult.ok();
        }
        return EgoResult.error("重命名失败");
    }

    @Override
    public EgoResult deleteContentCategoryById(long id) {
        try {
            int result = contentCategoryDubboService.deleteContentCategoryById(id);
            if (result == 1){
                return EgoResult.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EgoResult.error("删除节点失败");
    }
}
